<template>
  <table-structure
    v-if="isStruct"
    :categoryName="treeSelectedInfo?.categoryName"
    :tableInfo="importTableInfo"
  />
  <table-embed v-else />
</template>
<script setup>
import TableEmbed from './table-embed.vue'
import { useProvideContextRegistry, useDbType } from './provide-context.js'
import { toRef, computed, onActivated, onDeactivated, watch, ref } from 'vue'
import useActivated from '@/hooks/useActivated.js'
import { getFileInfo } from '@/api/db.js'
import TableStructure from './table-structure-embed.vue'
import { message as Message } from 'ant-design-vue'

/**
 * 外部参数
 */
let props = defineProps({
  groupId: { type: String },
  treeSelectedInfo: { type: Object },
  staticPermission: {
    type: Object,
  },
})

let isActivated = useActivated()

// /**
//  * 事件定义
//  */
// let emit = defineEmits(['update:groupId'])
// /**
//  * 双向绑定
//  */
// let groupId = useVModel(props, 'groupId', emit)
let groupId = toRef(props, 'groupId')

let treeSelectedInfo = toRef(props, 'treeSelectedInfo')
// let categoryName = computed(() => treeSelectedInfo.value?.categoryName)
useProvideContextRegistry({
  groupId,
  categoryId: groupId,
  treeSelectedInfo,
  useSearchQueryDataname: true,
  staticPermission: props.staticPermission,
})

/**
 * 获取是否是结构化数据
 */
let dbType = useDbType()
let isStruct = ref(false)
let importTableInfo = ref({})
let loading = ref(false)
watch(
  groupId,
  async parentId => {
    if (!parentId) return
    loading.value = true
    try {
      let { data } =
        (await getFileInfo(
          { parentId, pageNum: 1, pageSize: 10, keyword: '' },
          dbType,
        )) || {}
      let { struct, importTableInfoVO } = data
      isStruct.value = struct
      importTableInfo.value = importTableInfoVO
    } catch (e) {
      Message.error(`是否是结构化数据查询失败![${e.message}]`)
    }
    loading.value = false
  },
  { immediate: true },
)
</script>
<style scoped lang="less">
.title-wrap {
  display: flex;
  white-space: nowrap;
  align-items: center;
  span {
    margin-right: 12px;
  }
}
</style>
